/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.ScrollPaneConstants;

public class AboutDialog extends JPanel {

	public AboutDialog() {
		ImageIcon prionaceIcon = new ImageIcon(getClass().getResource(
				"/icons/prionace_logo_128x128.png"));
		JLabel prionace = new JLabel(prionaceIcon);
		JLabel title = new JLabel("Prionace");
		JLabel descriptive = new JLabel("A file navigator to manage your files");
		JLabel copyright = new JLabel(
				"Copyright \u00A9 2012 - Francivan Bezerra");
		JEditorPane license = null;
		JScrollPane scroll = null;

		try {
			license = new JEditorPane("text/html", readGPLHtml());
			scroll = new JScrollPane(license,
					ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
					ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		} catch (IOException e) {
			e.printStackTrace();
		}

		prionace.setAlignmentX(CENTER_ALIGNMENT);
		title.setAlignmentX(CENTER_ALIGNMENT);
		descriptive.setAlignmentX(CENTER_ALIGNMENT);
		copyright.setAlignmentX(CENTER_ALIGNMENT);
		scroll.setAlignmentX(CENTER_ALIGNMENT);

		title.setFont(title.getFont().deriveFont(20f));
		title.setFont(title.getFont().deriveFont(Font.BOLD));
		descriptive.setFont(descriptive.getFont().deriveFont(Font.BOLD));
		descriptive.setFont(descriptive.getFont().deriveFont(Font.ITALIC));

		license.setEditable(false);
		license.setCaretPosition(0);
		license.setPreferredSize(new Dimension(420, 180));

		add(prionace);
		add(title);
		add(descriptive);
		add(new JPanel());
		add(copyright);
		add(new JSeparator());
		add(scroll);

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
	}

	public String readGPLHtml() throws IOException {
		String line;
		StringBuffer gpl = new StringBuffer();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				getClass().getResourceAsStream("/GPLv3.html")));

		while ((line = reader.readLine()) != null) {
			gpl.append(line);
		}

		return gpl.toString();
	}
}
